/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.picasaconnector;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;

public class PicasaConnectorSettingView extends JFrame {

    /**
     * serial version.
     */
    private static final long serialVersionUID = 1L;

    PicasaConnector connector = null;

    private static final String KEY_TITLE = "setting.view.title";
    private static final String KEY_USER_NAME = "user.name";
    private static final String KEY_PASSWORD = "password";

    static String userName = null;
    static String password = null;

    /**
     * Create new setting view.
     * 
     * @param picasaConnectorActivator
     */
    public PicasaConnectorSettingView(PicasaConnector connector) {
        super(PicasaConnectorActivator.RB.getString(KEY_TITLE));
        this.connector = connector;
        initComponents();
    }

    private void initComponents() {
        final JFrame frame = this;

        // create components
        // final JLabel dirLabel = new
        // JLabel(PicasaConnectorActivator.RB.getString("dir.path"));

        final JLabel nameLabel = new JLabel(PicasaConnectorActivator.RB
                .getString(KEY_USER_NAME));
        final JTextField nameText = new JTextField();
        nameText.setPreferredSize(new Dimension(200, 20));

        final JLabel passLabel = new JLabel(PicasaConnectorActivator.RB
                .getString(KEY_PASSWORD));
        final JPasswordField passText = new JPasswordField();
        passText.setPreferredSize(new Dimension(200, 20));

        final JButton okButton = new JButton(PicasaConnectorActivator.RB
                .getString("ok"));
        final JButton applyButton = new JButton(PicasaConnectorActivator.RB
                .getString("apply"));
        final JButton cancelButton = new JButton(PicasaConnectorActivator.RB
                .getString("cancel"));

        final ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final Object source = e.getSource();
                if (source == okButton || source == applyButton) {
                    userName = nameText.getText();
                    char[] chars = passText.getPassword();
                    password = String.valueOf(chars);
                    init();
                }
                if (source == okButton || source == cancelButton) {
                    frame.dispose();
                }
            }
            void init() {
                // change authenticate info
                connector.isAuthenticated = false;
                connector.urlList = null;
            }
        };

        okButton.addActionListener(actionListener);
        applyButton.addActionListener(actionListener);
        cancelButton.addActionListener(actionListener);

        // set layout
        final GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        {
            // set horizontal group
            final ParallelGroup pg1 = layout
                    .createParallelGroup(Alignment.TRAILING);
            pg1.addComponent(nameLabel).addComponent(passLabel).addComponent(
                    okButton);
            final ParallelGroup pg2 = layout
                    .createParallelGroup(Alignment.TRAILING);
            pg2.addComponent(nameText).addComponent(passText).addComponent(
                    applyButton);
            final ParallelGroup pg3 = layout
                    .createParallelGroup(Alignment.TRAILING);
            pg3.addComponent(cancelButton);

            layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
                    pg1).addGroup(pg2).addGroup(pg3));
        }
        {
            // set vertical group
            final ParallelGroup pg1 = layout
                    .createParallelGroup(Alignment.BASELINE);
            pg1.addComponent(nameLabel).addComponent(nameText);

            final ParallelGroup pg2 = layout
                    .createParallelGroup(Alignment.BASELINE);
            pg2.addComponent(passLabel).addComponent(passText);

            final ParallelGroup pg3 = layout
                    .createParallelGroup(Alignment.BASELINE);
            pg3.addComponent(okButton).addComponent(applyButton).addComponent(
                    cancelButton);

            layout.setVerticalGroup(layout.createSequentialGroup()
                    .addGroup(pg1).addGroup(pg2).addGroup(pg3));
        }

        pack();
    }
}
